package com.blq.snblib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

/**
 * ================================================
 * 作者: SSNB
 * 日期: 2016/9/14
 * 描述:
 * 屏幕相关的辅助类
 * 屏幕获取宽高最好在{@link android.app.Activity#setContentView}之后调用，
 * 否者可能会出现宽高为0的状态
 * ================================================
 */
public class ScreenUtil {

    private ScreenUtil(){
        throw new IllegalAccessError("不能被创建，请使用静态方法");
    }

    /**
     * 获得屏幕的宽度
     * @param context 上下文对象
     * @return 屏幕宽度（px）
     */
    public static int getScreenWidth(Context context){
        DisplayMetrics metrics = getMetrics(context);
        return metrics.widthPixels;
    }

    /**
     * 获得屏幕的高度(包括状态栏)
     * @param context 上下方对象
     * @return 屏幕高度（px）
     */
    public static int getScreenHeight(Context context){
        DisplayMetrics metrics = getMetrics(context);
        return metrics.heightPixels;
    }

    private static DisplayMetrics getMetrics(Context context){
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

    final private static String dimenClassName = "com.android.internal.R$dimen";

    /**
     * 获得状态栏的高度
     * @param context 上下文对象
     * @return 返回状态栏高度，如果出错会返回-1
     */
    public static int getStatusHeight(Context context){
        int statusHeight = -1;
        Class<?> cls;
        try {
            cls = Class.forName(dimenClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.e("error","无法找到类:"+ dimenClassName);
            return statusHeight;
        }
        Object object;
        try {
            object = cls.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            Log.e("error","实例化类"+dimenClassName+"错误");
            return statusHeight;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.e("error","实例化类"+dimenClassName+"错误");
            return statusHeight;
        }
        int height;
        try {
            height = Integer.parseInt(cls.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            Log.e("error","获取status_bar_height的字段错误");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            Log.e("error","获取status_bar_height的字段错误");
        }
        return statusHeight;
    }

    /**
     * 截屏，包括状态栏
     * @param activity 当前activity
     * @return 返回截图
     */
    public static Bitmap screenshotWithStatusBar(Activity activity){

        //获取屏幕宽高
        DisplayMetrics metrics = getMetrics(activity);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        return screenshot(activity,0,0,width,height);
    }

    /**
     * 截屏，不包括状态栏
     * @return 返回截图
     */
    public static Bitmap screenshotWithoutStatusBar(Activity activity){

        //获取屏幕宽高
        DisplayMetrics metrics = getMetrics(activity);
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;

        //获取状态栏高度
        int statusHeight = getStatusHeight(activity);

        return screenshot(activity,0,statusHeight,width,height-statusHeight);
    }

    /**
     * 截屏
     * @param activity activity对象
     * @param x 截屏起始位置（距离左边）px
     * @param y 截屏其实位置（距离上边）px
     * @param width 截屏宽度（大于0） px
     * @param height 截屏高度（大于0） px
     * @return 返回截屏后的Bitmap 如果宽高小于0的话会返回null
     */
    public static Bitmap screenshot(Activity activity,int x,int y , int width , int height){
        if(width<=0||height<=0){
            return null;
        }
        //获取顶层view
        View view = activity.getWindow().getDecorView();
        view.buildDrawingCache();

        //允许当前窗口保存缓存信息
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(),x,y,width,height);

        //清楚缓存
        view.destroyDrawingCache();

        return bitmap;
    }

}
